package br.odb.bzk3.geometry;

public class Directions {
	public static final byte N = 0;
	public static final byte E = 1;
	public static final byte S = 2;
	public static final byte W = 3;
	public static final byte FLOOR = 4;
	public static final byte CEILING = 5;
	
	public static float normalizeAngle( float angle ) {
		float toReturn = angle;
		
		if ( toReturn < 0 ) {
			int tmp = (int) toReturn;
			tmp = 360 - tmp;
			toReturn = tmp;			
		}
		
		if ( toReturn > 360 ) {
			int tmp = (int) toReturn;
			tmp = tmp % 360;
			toReturn = tmp;
		}
		
		toReturn = 360.0f - toReturn;
		return toReturn + 45f;
	}

	public static int normalizeAngle( int angle ) {
		int toReturn = angle;
		
		if ( toReturn < 0 ) {
			int tmp = toReturn;
			tmp = 360 - tmp;
			toReturn = tmp;			
		}
		
		if ( toReturn > 360 ) {
			int tmp = toReturn;
			tmp = tmp % 360;
			toReturn = tmp;
		}	
		toReturn = 360 - toReturn;
		
		return toReturn + 45;
	}

	
	public static byte getDirection( int angle ) {
		return clamp( (byte) (( normalizeAngle( angle  ) ) / 90) );		
	}
	
	public static byte getOpposite( byte dir ) {
		return getRight( getRight ( dir ) );
	}
	
	public static byte clamp( byte dir ) {
		byte toReturn = dir;
		
		if ( toReturn  > W )
			return (byte) (toReturn % ( W + 1 ));

		if ( toReturn < N )
			toReturn = ( byte ) ( toReturn + ( W + 1 ) );
		
		return toReturn;
	}
	
	public static byte getLeft( byte dir ) {
		byte toReturn = dir;

		
		--toReturn;

		
		return clamp( toReturn );
	}
	
	public static byte getRight( byte dir ) {
		byte toReturn = dir;

		++toReturn;
		
		return clamp( toReturn );
	}

	public static byte getDirection(float angle ) {
		return clamp( (byte) (( normalizeAngle( angle  ) ) / 90.0f) );
	}
	

}
